package com.example.common.search;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.common.R;
import com.example.common.component.RecyclerOnScrollListener;
import com.example.common.databinding.ActivitySearchBinding;
import com.example.common.gamedetail.GameDetailActivity;
import com.example.common.network.IStartGameDetailActivity;

import java.text.ParseException;

public class SearchActivity extends AppCompatActivity {
    private static final String TAG = "SearchActivity";
    private ActivitySearchBinding binding;
    private SearchViewModel viewModel;
    private SearchRAdapter adapter;
    private String searchText = "";
    private Context context=this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化binding
        binding = DataBindingUtil.setContentView(this, R.layout.activity_search);


        //获取ViewModel实例
        viewModel = new ViewModelProvider(this, new ViewModelProvider
                .AndroidViewModelFactory(getApplication())).get(SearchViewModel.class);

        //adapter实例化
        adapter = new SearchRAdapter(viewModel);

        //设置点击事件
        adapter.iStartGameDetailActivity=new IStartGameDetailActivity() {
            @Override
            public void openById(Integer id) {
                Intent intent = new Intent(context, GameDetailActivity.class);
                intent.putExtra("ID", id);
                startActivity(intent);
            }
        };

        //recyclerView绑定
        binding.commonSearchRv.setLayoutManager(new LinearLayoutManager(this));
        binding.commonSearchRv.setAdapter(adapter);
        binding.commonSearchRv.setItemAnimator(null);//设置动画为null来解决闪烁问题

        //监控加载状态
        viewModel.getIsLoadDone().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    //todo 加载完成执行adapter刷新操作
                    adapter.notifyItemRangeChanged(0, adapter.getItemCount());
                }
            }
        });

        //上滑加载
        binding.commonSearchRv.addOnScrollListener(new RecyclerOnScrollListener() {
            @Override
            public void loadMore() {
                viewModel.getGameSearch(searchText);
            }

            @Override
            public void showTopData(int position) throws ParseException {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
        //获取传输过来的数据
        searchText = getIntent().getExtras().getString("SEARCH");
        //viewmodel再次初始化
        viewModel.reSet();
        //执行搜索
        viewModel.getGameSearch(searchText);
    }
}